/*
 * livedoor WEBサービスから天気予報RSSを読み込む
 */
// color backgroundColor = color(200,200,200);
color backgroundColor = color(255,128,64);

size(200,600);
background(backgroundColor);


XMLElement xml;

// Livedoor Weather Web Service / LWWS
try {
  xml = new XMLElement(this, "http://weather.livedoor.com/forecast/webservice/rest/v1?city=63&day=today");
  XMLElement imageUrl = xml.getChild("image/url");
  String s1 = imageUrl.getContent();
  println(s1);
  PImage weatherImg = loadImage(s1);

  XMLElement imageWidth = xml.getChild("image/width");
  String s3 = imageWidth.getContent();
  println("w:"+ s3);

  XMLElement imageHeight = xml.getChild("image/height");
  String s4 = imageHeight.getContent();
  println("h:"+s4);

  PImage img = createImage(int(s3),int(s4),ARGB);
  PImage img2 = createImage(int(s3),int(s4),ARGB);
  img.loadPixels();
  img2.loadPixels();
  weatherImg.loadPixels();

  color blendedColor;

  for ( int i=0 ; i < weatherImg.pixels.length ; i++){
    boolean isEdge = false ;
    // println ("   pixcel "+i + ": " + weatherImg.pixels[i]);
    
    //float sat = saturation(weatherImg.pixels[i]) ;
    //println("Saturation," + i + "," + sat);

    // if background (assume white)
    if ( weatherImg.pixels[i] == color(255,255,255) ){
      img.pixels[i] = color(255,0);
      img2.pixels[i] = color(255,0);
    } 
    else 
    {
      // left edge
      if ( i < weatherImg.pixels.length -1  && weatherImg.pixels[i+1] == color(255,255,255)) {
        isEdge = true;
      } 
      // right edge
      else if ( i > 0  && weatherImg.pixels[i-1] == color(255,255,255)) {
        isEdge = true;
      }
      // top edge
      else if ( (i - weatherImg.width) >= 0   && weatherImg.pixels[i-weatherImg.width] == color(255,255,255)) {
        isEdge = true;
      }
      // bottom edge
      else if (( (i + weatherImg.width) < weatherImg.pixels.length   && weatherImg.pixels[i+weatherImg.width] == color(255,255,255)) || (i + weatherImg.width) > weatherImg.pixels.length ) {
        isEdge = true;
      }
      if (isEdge) {
        float a_value = saturation(weatherImg.pixels[i]) ;
        // println ("i " + i + " / a_value:" +a_value); 
        img2.pixels[i] = color (red(weatherImg.pixels[i]), green(weatherImg.pixels[i]), blue(weatherImg.pixels[i]), a_value);
        //img2.pixels[i] = color (0);
        img.pixels[i] = weatherImg.pixels[i];
      }
      else {
        img.pixels[i] = weatherImg.pixels[i];
        img2.pixels[i] = weatherImg.pixels[i];
      }
    }
  }
  img.updatePixels();
  img2.updatePixels();
  image(weatherImg, 0, 0 , int(s3)*4, int(s4)*4);
  image(img,0,int(s4)*4, int(s3)*4, int(s4)*4);
  image(img2,0,int(s4)*8, int(s3)*4, int(s4)*4);
  image(weatherImg, 0, 400 );
  image(img, 0,400 + int(s4));
  image(img2,0,400 + int(s4)*2);

  fill(0,200,0);
  //text(" original ",55,20);
  // text(" with transparency ",55,70);
  noFill();


  XMLElement maxTemp = xml.getChild("temperature/max/celsius");
  String s2 = maxTemp.getContent();
  println(s2);
} 
catch (Exception e) {
  println("network error");
}







